package com.yfl.logistics.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfl.logistics.pojo.MsgObject;
import com.yfl.logistics.pojo.Permission;
import com.yfl.logistics.pojo.Role;
import com.yfl.logistics.pojo.User;
import com.yfl.logistics.service.impl.PermissionServiceImpl;
import com.yfl.logistics.service.impl.RoleServiceImpl;
import com.yfl.logistics.service.impl.UserServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-13
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleServiceImpl roleService;

    //注释管理员表
    @Autowired
    private UserServiceImpl userService;

    //权限页面跳转
    @GetMapping("/rolePage")
    public String rolePage() {

        return "rolePage";
    }


    //分页查询操作
    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    String keyword) {

        //创建分页插件
        IPage<Role> page = new Page<>(pageNum, pageSize);

        //创建条件查询对象
        QueryWrapper qw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)) {
            qw.like("c.name", keyword);
            qw.or();
            qw.like("p.name", keyword);
        }

        IPage<Role> iPage = roleService.page(page,qw);

        Map<String, Object> map = new HashMap<>();
        map.put("total", iPage.getTotal());
        map.put("rows", iPage.getRecords());

        return map;
    }

    //删除操作

    @GetMapping("/delete")
    @ResponseBody
    public MsgObject delect(Integer roleId) {

        QueryWrapper<User> uqw=new QueryWrapper<>();
        uqw.eq("role_id",roleId);
        List<User> users = userService.list(uqw);
        if (users.size()>0){
            MsgObject msgObject = new MsgObject();
                msgObject.setCode(0);
                msgObject.setMsg("无法删除，此角色拥有管理员");
                return msgObject;
        }


        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq("role_id", roleId);
        boolean remove = roleService.remove(qw);
        MsgObject msgObject = new MsgObject();
        if (remove) {
            msgObject.setCode(1);
            msgObject.setMsg("删除成功");
        } else {
            msgObject.setCode(0);
            msgObject.setMsg("失败");
        }

        return msgObject;
    }

    //添加编辑页面跳转
    @GetMapping("/roleEdit")
    public String roleEdit(Integer roleId, Model m) {


        QueryWrapper<Role> qw =new QueryWrapper<>();
        qw.eq("role_id",roleId);
        Role role = roleService.getOne(qw);

        System.out.println("修改数据回显"+role);

        //编辑数据回显
        m.addAttribute("role",role);

        return "roleEdit";
    }


    //添加/更新角色
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public MsgObject saveOrUpdateRole(Role role) {

        System.out.println("角色"+role);

        boolean saveOrUpdate = roleService.saveOrUpdate(role);

        MsgObject msgObject = new MsgObject();
        if (saveOrUpdate) {
            msgObject.setMsg("操作成功");
            msgObject.setCode(1);
        } else {
            msgObject.setMsg("操作失败");
            msgObject.setCode(2);
        }

        return msgObject;
    }

    //批量删除
    @GetMapping("/delroles")
    @ResponseBody
    public MsgObject delroles(@RequestParam Integer[] roleIds) {

        boolean removeByIds = roleService.removeByIds(Arrays.asList(roleIds));

        MsgObject msgObject = new MsgObject();
        if (removeByIds) {
            msgObject.setMsg("批量删除成功");
            msgObject.setCode(1);
        } else {
            msgObject.setMsg("批量删除失败");
            msgObject.setCode(2);
        }
        return msgObject;
    }
}

